﻿Imports System.IO
Module SavePlayer
    Sub saveplayer(Optional ByVal PlayerNum As String = "1")
        Dim CurItem As Integer = 0
        Dim BlankItem As String = ""
        MainProgram.User_Data = "<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding=" + Chr(34) + "UTF-8" + Chr(34) + "?>"
        MainProgram.User_Data += Chr(13) + "<?xml-stylesheet type=" + Chr(34) + "text/xsl" + Chr(34) + " href=" + Chr(34) + "character.xsl" + Chr(34) + "?>"
        MainProgram.User_Data += Chr(13) + "<savefile>"
        MainProgram.User_Data += Chr(13) + "    <playername>" + MainProgram.User_Name + "</playername>"
        MainProgram.User_Data += Chr(13) + "    <password>" + MainProgram.User_Password + "</password>"
        'don't allow the player to save their location to a random map set to beginning map
        If MainProgram.UserLocation_Map = 0 Then BlankItem = "RuinsOfVharmas" Else BlankItem = MainProgram.UserLocation_MapString
        MainProgram.User_Data += Chr(13) + "    <currentmap>" + BlankItem + "</currentmap>"
        'don't allow the player to save their location to a random map set to beginning location
        If MainProgram.UserLocation_Sector = 0 Then BlankItem = "3" Else BlankItem = LTrim(Str(MainProgram.UserLocation_Sector))
        MainProgram.User_Data += Chr(13) + "    <currentsector>" + BlankItem + "</currentsector>"
        MainProgram.User_Data += Chr(13) + "    <experience>" + LTrim(Str(MainProgram.User_Experience)) + "</experience>"
        MainProgram.User_Data += Chr(13) + "    <practicepoints>" + LTrim(Str(MainProgram.User_PracticePoints)) + "</practicepoints>"
        MainProgram.User_Data += Chr(13) + "    <trainingpoints>" + LTrim(Str(MainProgram.User_TrainingPoints)) + "</trainingpoints>"
        MainProgram.User_Data += Chr(13) + "    <strengthmodded>" + LTrim(Str(MainProgram.User_Strengthmod)) + "</strengthmodded>"
        MainProgram.User_Data += Chr(13) + "    <strengthcurrent>" + LTrim(Str(MainProgram.User_Strength)) + "</strengthcurrent>"
        MainProgram.User_Data += Chr(13) + "    <strengthmax>" + LTrim(Str(MainProgram.User_StrengthMax)) + "</strengthmax>"
        MainProgram.User_Data += Chr(13) + "    <dexteritymodded>" + LTrim(Str(MainProgram.User_Dexteritymod)) + "</dexteritymodded>"
        MainProgram.User_Data += Chr(13) + "    <dexteritycurrent>" + LTrim(Str(MainProgram.User_Dexterity)) + "</dexteritycurrent>"
        MainProgram.User_Data += Chr(13) + "    <dexteritymax>" + LTrim(Str(MainProgram.User_DexterityMax)) + "</dexteritymax>"
        MainProgram.User_Data += Chr(13) + "    <constitutionmodded>" + LTrim(Str(MainProgram.User_Constitutionmod)) + "</constitutionmodded>"
        MainProgram.User_Data += Chr(13) + "    <constitutioncurrent>" + LTrim(Str(MainProgram.User_Constitution)) + "</constitutioncurrent>"
        MainProgram.User_Data += Chr(13) + "    <constitutionmax>" + LTrim(Str(MainProgram.User_ConstitutionMax)) + "</constitutionmax>"
        MainProgram.User_Data += Chr(13) + "    <intelligencemodded>" + LTrim(Str(MainProgram.User_Intelligencemod)) + "</intelligencemodded>"
        MainProgram.User_Data += Chr(13) + "    <intelligencecurrent>" + LTrim(Str(MainProgram.User_Intelligence)) + "</intelligencecurrent>"
        MainProgram.User_Data += Chr(13) + "    <intelligencemax>" + LTrim(Str(MainProgram.User_IntelligenceMax)) + "</intelligencemax>"
        MainProgram.User_Data += Chr(13) + "    <wisdommodded>" + LTrim(Str(MainProgram.User_Wisdommod)) + "</wisdommodded>"
        MainProgram.User_Data += Chr(13) + "    <wisdomcurrent>" + LTrim(Str(MainProgram.User_Wisdom)) + "</wisdomcurrent>"
        MainProgram.User_Data += Chr(13) + "    <wisdommax>" + LTrim(Str(MainProgram.User_WisdomMax)) + "</wisdommax>"
        MainProgram.User_Data += Chr(13) + "    <charismamodded>" + LTrim(Str(MainProgram.User_Charismamod)) + "</charismamodded>"
        MainProgram.User_Data += Chr(13) + "    <charismacurrent>" + LTrim(Str(MainProgram.User_Charisma)) + "</charismacurrent>"
        MainProgram.User_Data += Chr(13) + "    <charismamax>" + LTrim(Str(MainProgram.User_CharismaMax)) + "</charismamax>"
        MainProgram.User_Data += Chr(13) + "    <luckmodded>" + LTrim(Str(MainProgram.User_Luckmod)) + "</luckmodded>"
        MainProgram.User_Data += Chr(13) + "    <luckcurrent>" + LTrim(Str(MainProgram.User_Luck)) + "</luckcurrent>"
        MainProgram.User_Data += Chr(13) + "    <luckmax>" + LTrim(Str(MainProgram.User_LuckMax)) + "</luckmax>"
        MainProgram.User_Data += Chr(13) + "    <wealthvern>" + MainProgram.User_Vern + "</wealthvern>"
        MainProgram.User_Data += Chr(13) + "    <wealthdover>" + MainProgram.User_Dover + "</wealthdover>"
        MainProgram.User_Data += Chr(13) + "    <wealthsovern>" + MainProgram.User_Sovern + "</wealthsovern>"
        MainProgram.User_Data += Chr(13) + "    <bankvern>" + MainProgram.User_BankVern + "</bankvern>"
        MainProgram.User_Data += Chr(13) + "    <bankdover>" + MainProgram.User_BankDover + "</bankdover>"
        MainProgram.User_Data += Chr(13) + "    <banksovern>" + MainProgram.User_BankSovern + "</banksovern>"
        MainProgram.User_Data += Chr(13) + "    <hitpointscurrent>" + MainProgram.User_CurHP + "</hitpointscurrent>"
        MainProgram.User_Data += Chr(13) + "    <hitpointsmaximum>" + MainProgram.User_MaxHP + "</hitpointsmaximum>"
        MainProgram.User_Data += Chr(13) + "    <willpowercurrent>" + MainProgram.User_CurWP + "</willpowercurrent>"
        MainProgram.User_Data += Chr(13) + "    <willpowermaximum>" + MainProgram.User_MaxWP + "</willpowermaximum>"
        MainProgram.User_Data += Chr(13) + "    <staminacurrent>" + MainProgram.User_CurSP + "</staminacurrent>"
        MainProgram.User_Data += Chr(13) + "    <staminamaximum>" + MainProgram.User_MaxSP + "</staminamaximum>"
        MainProgram.User_Data += Chr(13) + "    <class>" + MainProgram.User_Class + "</class>"
        MainProgram.User_Data += Chr(13) + "    <race>" + MainProgram.User_Race + "</race>"
        MainProgram.User_Data += Chr(13) + "    <age>" + MainProgram.User_Age + "</age>"
        MainProgram.User_Data += Chr(13) + "    <birth>" + MainProgram.User_Birthday + "</birth>"
        MainProgram.User_Data += Chr(13) + "    <gender>" + MainProgram.User_Gender + "</gender>"
        MainProgram.User_Data += Chr(13) + "    <province>" + MainProgram.User_HomeProvince + "</province>"
        MainProgram.User_Data += Chr(13) + "    <rode>" + MainProgram.User_Rode + "</rode>"
        MainProgram.User_Data += Chr(13) + "    <weight>" + MainProgram.User_Weight + "</weight>"
        MainProgram.User_Data += Chr(13) + "    <height>" + MainProgram.User_Height + "</height>"
        MainProgram.User_Data += Chr(13) + "    <naturalarmorclass>" + LTrim(Str(MainProgram.User_NaturalAC)) + "</naturalarmorclass>"
        MainProgram.User_Data += Chr(13) + "    <level>" + LTrim(Str(MainProgram.User_Level)) + "</level>"
        If MainProgram.UserEquip_Mainhand = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Mainhand
        MainProgram.User_Data += Chr(13) + "    <mainhand number=" + Chr(34) + MainProgram.UserEquip_MainhandNUM + Chr(34) + ">" + BlankItem + "</mainhand>"
        If MainProgram.UserEquip_Offhand = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Offhand
        MainProgram.User_Data += Chr(13) + "    <offhand number=" + Chr(34) + MainProgram.UserEquip_OffhandNUM + Chr(34) + ">" + BlankItem + "</offhand>"
        If MainProgram.UserEquip_Range = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Range
        MainProgram.User_Data += Chr(13) + "    <ranged number=" + Chr(34) + MainProgram.UserEquip_RangeNUM + Chr(34) + ">" + BlankItem + "</ranged>"
        If MainProgram.UserEquip_Head = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Head
        MainProgram.User_Data += Chr(13) + "    <head number=" + Chr(34) + MainProgram.UserEquip_HeadNUM + Chr(34) + ">" + BlankItem + "</head>"
        If MainProgram.UserEquip_Neck = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Neck
        MainProgram.User_Data += Chr(13) + "    <neck number=" + Chr(34) + MainProgram.UserEquip_NeckNUM + Chr(34) + ">" + BlankItem + "</neck>"
        If MainProgram.UserEquip_Shoulders = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Shoulders
        MainProgram.User_Data += Chr(13) + "    <shoulder number=" + Chr(34) + MainProgram.UserEquip_ShouldersNUM + Chr(34) + ">" + BlankItem + "</shoulder>"
        If MainProgram.UserEquip_Chest = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Chest
        MainProgram.User_Data += Chr(13) + "    <chest number=" + Chr(34) + MainProgram.UserEquip_ChestNUM + Chr(34) + ">" + BlankItem + "</chest>"
        If MainProgram.UserEquip_Back = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Back
        MainProgram.User_Data += Chr(13) + "    <back number=" + Chr(34) + MainProgram.UserEquip_BackNUM + Chr(34) + ">" + BlankItem + "</back>"
        If MainProgram.UserEquip_Arms = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Arms
        MainProgram.User_Data += Chr(13) + "    <arm number=" + Chr(34) + MainProgram.UserEquip_ArmsNUM + Chr(34) + ">" + BlankItem + "</arm>"
        If MainProgram.UserEquip_Hands = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Hands
        MainProgram.User_Data += Chr(13) + "    <hand number=" + Chr(34) + MainProgram.UserEquip_HandsNUM + Chr(34) + ">" + BlankItem + "</hand>"
        If MainProgram.UserEquip_Wrists = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Wrists
        MainProgram.User_Data += Chr(13) + "    <wrists number=" + Chr(34) + MainProgram.UserEquip_WristsNUM + Chr(34) + ">" + BlankItem + "</wrists>"
        If MainProgram.UserEquip_LFinger = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_LFinger
        MainProgram.User_Data += Chr(13) + "    <leftfinger number=" + Chr(34) + MainProgram.UserEquip_LFingerNUM + Chr(34) + ">" + BlankItem + "</leftfinger>"
        If MainProgram.UserEquip_RFinger = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_RFinger
        MainProgram.User_Data += Chr(13) + "    <rightfinger number=" + Chr(34) + MainProgram.UserEquip_RFingerNUM + Chr(34) + ">" + BlankItem + "</rightfinger>"
        If MainProgram.UserEquip_Waist = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Waist
        MainProgram.User_Data += Chr(13) + "    <belt number=" + Chr(34) + MainProgram.UserEquip_WaistNUM + Chr(34) + ">" + BlankItem + "</belt>"
        If MainProgram.UserEquip_Legs = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Legs
        MainProgram.User_Data += Chr(13) + "    <legs number=" + Chr(34) + MainProgram.UserEquip_LegsNUM + Chr(34) + ">" + BlankItem + "</legs>"
        If MainProgram.UserEquip_Boots = "" Then BlankItem = "Nothing" Else BlankItem = MainProgram.UserEquip_Boots
        MainProgram.User_Data += Chr(13) + "    <feet number=" + Chr(34) + MainProgram.UserEquip_BootsNUM + Chr(34) + ">" + BlankItem + "</feet>"
        MainProgram.User_Data += Chr(13) + "    <citizenship>" + MainProgram.User_Citizenship + "</citizenship>"
        MainProgram.User_Data += Chr(13) + "    <clan>" + MainProgram.User_Clan + "</clan>"
        MainProgram.User_Data += Chr(13) + "    <clanrank>" + MainProgram.User_ClanRank + "</clanrank>"
        MainProgram.User_Data += Chr(13) + "    <tradesguild>" + MainProgram.User_Tradesguild + "</tradesguild>"
        MainProgram.User_Data += Chr(13) + "    <tradesguildrank>" + MainProgram.User_TradesguildRank + "</tradesguildrank>"
        MainProgram.User_Data += Chr(13) + "    <loyalty>" + MainProgram.User_Loyalty + "</loyalty>"
        MainProgram.User_Data += Chr(13) + "    <languagecommon>" + LTrim(Str(MainProgram.User_Language_Common)) + "</languagecommon>"
        MainProgram.User_Data += Chr(13) + "    <languagenarsik>" + LTrim(Str(MainProgram.User_Language_Narsik)) + "</languagenarsik>"
        MainProgram.User_Data += Chr(13) + "    <languagenorthern>" + LTrim(Str(MainProgram.User_Language_Northern)) + "</languagenorthern>"
        MainProgram.User_Data += Chr(13) + "    <languagedraconian>" + LTrim(Str(MainProgram.User_Language_Draconian)) + "</languagedraconian>"
        MainProgram.User_Data += Chr(13) + "    <languagecelestial>" + LTrim(Str(MainProgram.User_Language_Celestial)) + "</languagecelestial>"
        MainProgram.User_Data += Chr(13) + "    <languageinfernal>" + LTrim(Str(MainProgram.User_Language_Infernal)) + "</languageinfernal>"
        MainProgram.User_Data += Chr(13) + "    <languagearcanic>" + LTrim(Str(MainProgram.User_Language_Arcanic)) + "</languagearcanic>"
        MainProgram.User_Data += Chr(13) + "    <fireresistance>" + LTrim(Str(MainProgram.User_Resistance_Fire)) + "</fireresistance>"
        MainProgram.User_Data += Chr(13) + "    <waterresistance>" + LTrim(Str(MainProgram.User_Resistance_Water)) + "</waterresistance>"
        MainProgram.User_Data += Chr(13) + "    <airresistance>" + LTrim(Str(MainProgram.User_Resistance_Air)) + "</airresistance>"
        MainProgram.User_Data += Chr(13) + "    <earthresistance>" + LTrim(Str(MainProgram.User_Resistance_Earth)) + "</earthresistance>"
        MainProgram.User_Data += Chr(13) + "    <spiritresistance>" + LTrim(Str(MainProgram.User_Resistance_Spirit)) + "</spiritresistance>"
        MainProgram.User_Data += Chr(13) + "    <fireamplitude>" + LTrim(Str(MainProgram.User_Amplification_Fire)) + "</fireamplitude>"
        MainProgram.User_Data += Chr(13) + "    <wateramplitude>" + LTrim(Str(MainProgram.User_Amplification_Water)) + "</wateramplitude>"
        MainProgram.User_Data += Chr(13) + "    <airamplitude>" + LTrim(Str(MainProgram.User_Amplification_Air)) + "</airamplitude>"
        MainProgram.User_Data += Chr(13) + "    <earthamplitude>" + LTrim(Str(MainProgram.User_Amplification_Earth)) + "</earthamplitude>"
        MainProgram.User_Data += Chr(13) + "    <spiritamplitude>" + LTrim(Str(MainProgram.User_Amplification_Spirit)) + "</spiritamplitude>"
        MainProgram.User_Data += Chr(13) + "    <subelementalfireresistance>" + LTrim(Str(MainProgram.User_SubResistance_Fire)) + "</subelementalfireresistance>"
        MainProgram.User_Data += Chr(13) + "    <subelementalwaterresistance>" + LTrim(Str(MainProgram.User_SubResistance_Water)) + "</subelementalwaterresistance>"
        MainProgram.User_Data += Chr(13) + "    <subelementalairresistance>" + LTrim(Str(MainProgram.User_SubResistance_Air)) + "</subelementalairresistance>"
        MainProgram.User_Data += Chr(13) + "    <subelementalearthresistance>" + LTrim(Str(MainProgram.User_SubResistance_Earth)) + "</subelementalearthresistance>"
        MainProgram.User_Data += Chr(13) + "    <subelementalspiritresistance>" + LTrim(Str(MainProgram.User_SubResistance_Spirit)) + "</subelementalspiritresistance>"
        MainProgram.User_Data += Chr(13) + "    <subelementalfireamplitude>" + LTrim(Str(MainProgram.User_SubAmplification_Fire)) + "</subelementalfireamplitude>"
        MainProgram.User_Data += Chr(13) + "    <subelementalwateramplitude>" + LTrim(Str(MainProgram.User_SubAmplification_Water)) + "</subelementalwateramplitude>"
        MainProgram.User_Data += Chr(13) + "    <subelementalairamplitude>" + LTrim(Str(MainProgram.User_SubAmplification_Air)) + "</subelementalairamplitude>"
        MainProgram.User_Data += Chr(13) + "    <subelementalearthamplitude>" + LTrim(Str(MainProgram.User_SubAmplification_Earth)) + "</subelementalearthamplitude>"
        MainProgram.User_Data += Chr(13) + "    <subelementalspiritamplitude>" + LTrim(Str(MainProgram.User_SubAmplification_Spirit)) + "</subelementalspiritamplitude>"
        'recurse through items until inventory space is empty to count inventory items
        For CurItem = 0 To 39 Step 1 : If MainProgram.User_Inventory(CurItem) = "" Then : Exit For : End If : Next
        'curitem is the total number of inventory items
        MainProgram.User_Data += Chr(13) + "    <inventory number=" + Chr(34) + LTrim(Str(CurItem)) + Chr(34) + ">"
        If CurItem > 0 Then
            Dim MaxItem As Integer = CurItem
            For CurItem = 0 To MaxItem - 1 Step 1
                MainProgram.User_Data += Chr(13) + "        <item number=" + Chr(34) + LTrim(Str(CurItem)) + Chr(34) + ">" + MainProgram.User_Inventory(CurItem) + "</item>"
            Next
        End If
        MainProgram.User_Data += Chr(13) + "    </inventory>"
        'recurse through items until inventory space is empty to count inventory items
        For CurItem = 0 To 39 Step 1 : If MainProgram.User_Proficiency(CurItem) = "" Then : Exit For : End If : Next
        'curitem is the total number of inventory items
        MainProgram.User_Data += Chr(13) + "    <proficiencies number=" + Chr(34) + LTrim(Str(CurItem)) + Chr(34) + ">"
        If CurItem > 0 Then
            Dim MaxItem As Integer = CurItem
            For CurItem = 0 To MaxItem - 1 Step 1
                MainProgram.User_Data += Chr(13) + "        <proficiency name=" + Chr(34) + MainProgram.User_Proficiency(CurItem) + Chr(34) + ">" + MainProgram.User_ProficiencyDesc(CurItem) + "</proficiency>"
            Next
        End If
        MainProgram.User_Data += Chr(13) + "    </proficiencies>"
        'recurse through items until inventory space is empty to count inventory items
        For CurItem = 0 To 39 Step 1 : If MainProgram.AbilityName(CurItem) = "" Then : Exit For : End If : Next
        'curitem is the total number of inventory items
        MainProgram.User_Data += Chr(13) + "    <abilities number=" + Chr(34) + LTrim(Str(CurItem)) + Chr(34) + ">"
        If CurItem > 0 Then
            Dim MaxItem As Integer = CurItem
            For CurItem = 0 To MaxItem - 1 Step 1
                MainProgram.User_Data += Chr(13) + "        <ability name=" + Chr(34) + MainProgram.AbilityName(CurItem) + Chr(34) + " level=" + Chr(34) + LTrim(Str(MainProgram.AbilityLevel(CurItem))) + Chr(34) + ">" + LTrim(Str(MainProgram.AbilityLearned(CurItem))) + "</ability>"
            Next
        End If
        MainProgram.User_Data += Chr(13) + "    </abilities>"
        MainProgram.User_Data += Chr(13) + "</savefile>"
        Dim test As String
        test = MainProgram.DirPlayer + MainProgram.CapitalizeEachWord(MainProgram.User_Name) + ".xml"
        If MainProgram.FileExists(MainProgram.DirPlayer + MainProgram.CapitalizeEachWord(MainProgram.User_Name) + ".xml") = False Then
            Dim fs As FileStream
            fs = File.Create(MainProgram.DirPlayer + MainProgram.CapitalizeEachWord(MainProgram.User_Name) + ".xml")
            fs.Close()
        End If
        MainProgram.SaveTextToFile(MainProgram.User_Data, MainProgram.DirPlayer + MainProgram.CapitalizeEachWord(MainProgram.User_Name) + ".xml", MainProgram.sErr)
        MainProgram.SND("/YSaved character locally!")
        'now put the save file on the web server
        If PlayerNum <> "Create" Then 'don't save the character to the server if the character was just created
            CharacterUpdates.GetUpdatesList()
            MainProgram.SND("/YSaved character to the server!")
        End If
    End Sub
End Module
